<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input v-model="queryParams.orderNumber" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="服务名称" prop="spuName">
              <el-input v-model="queryParams.spuName" placeholder="请输入服务名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="律所名称" prop="lawFirmName">
              <el-input v-model="queryParams.lawFirmName" placeholder="请输入律所名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="评价时间" prop="reviewTime">
              <el-date-picker clearable
                              v-model="dateRange"
                              type="daterange"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择评价时间"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['lawyer:spuComment:add']">新增</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Check" :disabled="multiple" @click="handleUpdate()" v-hasPermi="['lawyer:spuComment:edit']">通过</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['trade:comment:remove']">删除</el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['lawyer:spuComment:export']">导出</el-button>-->
<!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="spuCommentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键ID" align="center" prop="id" v-if="false" />
        <el-table-column label="用户昵称" align="center" prop="nickname" />
        <el-table-column label="用户头像" align="center" prop="avatar">
          <template #default="scope">
            <image-preview :src="scope.row.avatar" fit="cover" style="width: 59px; height:59px; "/>
          </template>
        </el-table-column>
        <el-table-column label="订单编号" align="center" prop="orderNumber" />
        <el-table-column label="服务名称" align="center" prop="spuName" />
<!--        <el-table-column label="评论状态" align="center" prop="status">-->
<!--          <template #default="scope">-->
<!--            <el-tag v-if="scope.row.status === '0'">待审核</el-tag>-->
<!--            <el-tag type="success" v-else>通过</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="综合评价" align="center" prop="overallRating">
          <template #default="scope">
            <el-rate v-model="scope.row.overallRating" :disabled="true" />
          </template>
        </el-table-column>
        <el-table-column label="评价内容" align="center" prop="reviewContent" />
        <el-table-column label="评价时间" align="center" prop="reviewTime" width="180" />
        <el-table-column label="律所名称" align="center" prop="lawFirmName" />
        <el-table-column label="律师印象标签" align="center" prop="reviewTag" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="InfoFilled" @click="handleUpdate(scope.row)" v-hasPermi="['trade:comment:query']"></el-button>
            </el-tooltip>
<!--            <el-tooltip v-if="scope.row.status === '0'" content="通过" placement="top" >-->
<!--              <el-button link type="primary" icon="Check" @click="handlePassedComment(scope.row)" v-hasPermi="['trade:comment:pass']"></el-button>-->
<!--            </el-tooltip>-->
            <el-tooltip content="删除" placement="top">
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['trade:comment:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改商品评价信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="839px" append-to-body>
      <el-form ref="spuCommentFormRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="用户头像" prop="avatar">
          <image-preview :src="form.avatar" style="width:59px; height:59px; "/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="form.nickname" readonly/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" readonly/>
        </el-form-item>
        <el-form-item label="服务名称" prop="spuName">
          <el-input v-model="form.spuName" readonly/>
        </el-form-item>
        <el-form-item label="律师/律所名称" prop="lawFirmName">
          <el-input v-model="form.lawFirmName" readonly/>
        </el-form-item>
        <el-form-item label="律师印象标签" prop="reviewTag">
          <el-input v-model="form.reviewTag" type="textarea" :rows="3" readonly/>
        </el-form-item>
        <el-form-item label="综合评价" prop="overallRating">
          <el-rate v-model="form.overallRating" disabled/>
        </el-form-item>
        <el-form-item label="专业水平" prop="competenceRating">
          <el-rate v-model="form.competenceRating" disabled/>
        </el-form-item>
        <el-form-item label="工作态度" prop="attitudeRating">
          <el-rate v-model="form.attitudeRating" disabled/>
        </el-form-item>
        <el-form-item label="工作效率" prop="efficiencyRating">
          <el-rate v-model="form.efficiencyRating" disabled/>
        </el-form-item>
        <el-form-item label="评价内容" prop="reviewContent">
         <el-input v-model="form.reviewContent" type="textarea" :rows="3" readonly/>
        </el-form-item>
        <el-form-item label="评价时间" prop="reviewTime">
            <el-text>{{ form.reviewTime }}</el-text>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="cancel">关 闭</el-button>
<!--          <el-button @click="cancel">取 消</el-button>-->
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderComment" lang="ts">
import { listSpuComment, getSpuComment, addSpuComment, updateSpuComment, delSpuComment, passedSpuComment } from '@/api/spu/comment';
import { SpuCommentVo, SpuCommentForm, SpuCommentQuery } from '@/api/spu/comment/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const spuCommentList = ref<SpuCommentVo[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const spuCommentFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SpuCommentForm = {
  id: undefined,
  wxUserId: undefined,
  nickname: undefined,
  avatar: undefined,
  anonymous: undefined,
  orderId: undefined,
  orderItemId: undefined,
  spuId: undefined,
  spuName: undefined,
  overallRating: undefined,
  competenceRating: undefined,
  attitudeRating: undefined,
  efficiencyRating: undefined,
  averageRating: undefined,
  reviewContent: undefined,
  reviewTime: undefined,
  lawFirmId: undefined,
  reviewTag: undefined,
  replyStatus: undefined,
  replyUserId: undefined,
  replyContent: undefined,
  replyTime: undefined,
  status: undefined,
  visible: undefined,
  phoneNumber: undefined,
  lawFirmName: undefined,
  lawFirmLogo: undefined,
  orderNumber: undefined
}
const data = reactive<PageData<SpuCommentForm, SpuCommentQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    wxUserId: undefined,
    nickname: undefined,
    avatar: undefined,
    anonymous: undefined,
    orderId: undefined,
    orderItemId: undefined,
    spuId: undefined,
    spuName: undefined,
    overallRating: undefined,
    competenceRating: undefined,
    attitudeRating: undefined,
    efficiencyRating: undefined,
    averageRating: undefined,
    reviewContent: undefined,
    reviewTime: undefined,
    lawFirmId: undefined,
    reviewTag: undefined,
    replyStatus: undefined,
    replyUserId: undefined,
    replyContent: undefined,
    replyTime: undefined,
    status: undefined,
    visible: undefined,
    phoneNumber: undefined,
    lawFirmName: undefined,
    lawFirmLogo: undefined,
    orderNumber: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键ID不能为空", trigger: "blur" }
    ],
    wxUserId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    nickname: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
    avatar: [
      { required: true, message: "用户头像不能为空", trigger: "blur" }
    ],
    anonymous: [
      { required: true, message: "是否匿名不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "订单ID不能为空", trigger: "blur" }
    ],
    orderItemId: [
      { required: true, message: "交易订单项编号关联不能为空", trigger: "blur" }
    ],
    spuId: [
      { required: true, message: "商品ID不能为空", trigger: "blur" }
    ],
    spuName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    overallRating: [
      { required: true, message: "综合评价不能为空", trigger: "blur" }
    ],
    competenceRating: [
      { required: true, message: "专业水平不能为空", trigger: "blur" }
    ],
    attitudeRating: [
      { required: true, message: "工作态度不能为空", trigger: "blur" }
    ],
    efficiencyRating: [
      { required: true, message: "工作效率不能为空", trigger: "blur" }
    ],
    averageRating: [
      { required: true, message: "平均分不能为空", trigger: "blur" }
    ],
    reviewContent: [
      { required: true, message: "评价内容不能为空", trigger: "blur" }
    ],
    reviewTime: [
      { required: true, message: "评价时间不能为空", trigger: "blur" }
    ],
    lawFirmId: [
      { required: true, message: "律所ID不能为空", trigger: "blur" }
    ],
    reviewTag: [
      { required: true, message: "律师印象标签不能为空", trigger: "blur" }
    ],
    replyStatus: [
      { required: true, message: "商家是否回复不能为空", trigger: "change" }
    ],
    replyUserId: [
      { required: true, message: "回复人ID不能为空", trigger: "blur" }
    ],
    replyContent: [
      { required: true, message: "回复内容不能为空", trigger: "blur" }
    ],
    replyTime: [
      { required: true, message: "回复时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "评论状态不能为空", trigger: "change" }
    ],
    visible: [
      { required: true, message: "评价是否可见不能为空", trigger: "blur" }
    ],
    phoneNumber: [
      { required: true, message: "用户手机号不能为空", trigger: "blur" }
    ],
    lawFirmName: [
      { required: true, message: "律所名称不能为空", trigger: "blur" }
    ],
    lawFirmLogo: [
      { required: true, message: "律所logo不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品评价信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSpuComment(proxy?.addDateRange(queryParams.value, dateRange.value));
  spuCommentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  spuCommentFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SpuCommentVo[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '评价信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: SpuCommentVo) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getSpuComment(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '评价信息';
};

/** 提交按钮 */
const submitForm = () => {
  spuCommentFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSpuComment(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addSpuComment(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SpuCommentVo) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除当前选中的评论吗？').finally(() => loading.value = false);
  await delSpuComment(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const handlePassedComment = async (row?: SpuCommentVo) => {
  const _id = row?.id || ids.value[0];
  await proxy?.$modal.confirm('是否确认通过当前选中的评论吗？').finally(() => loading.value = false);
  await passedSpuComment(_id);
  proxy?.$modal.msgSuccess('操作成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('lawyer/spuComment/export', {
    ...queryParams.value
  }, `SpuComment_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
